﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
using System.Reflection;
using Newtonsoft.Json;

namespace E02
{
    /// <summary>
    /// 同步客户档案
    /// </summary>
    public class A05 : ISYNChildGet
    {
        public void Code(ISYN handler, ref CodeResult result, SYNRight user, SerialType st, String baseNo, String billId)
        {
            try
            {
                result.Code = "E02.A05";
                List<E02A05P> ht = new List<E02A05P>();
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NVarChar, 50);
                sps[0].Value = billId;
                string sql = " select FID BillId,FBILLNO Number,case when FDate = '' then '1990-01-01 00:00:00' when FDate IS null then '1990-01-01 00:00:00' else FDate end BuildDate,b.F_102 CusName,FTEXT PassWord,FTEXT1 LinkMan,FNOTE Remark,FCHECKBOX IsApp,FCHECKBOX1 IsIMEI " +
                             " ,(select FText5 from CardServer.dbo.KEW_DataBaseEntry where FNumber = " + baseNo + " ) FiUnitID from KEW_SALESGUESTS a left join t_Organization b on a.FBASE = b.FItemID where FID = @FID ";
                sql = sql + " select FEntryID BillId,FTEXT2 IMEI from KEW_SALESGUESTSIMEI where FID = @FID";
                Recordset rs = handler.Data.ExeProcedure(baseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows.Count == 1)
                    {
                        DataRow row = rs.Data.Tables[0].Rows[0];
                        if (row["CusName"] == null || row["CusName"].ToString().Equals(""))
                        {
                            result.Result = EnumResult.None;
                            result.Message = String.Format("客户档案数据提取错误:{0}", "请先配置基础资料客户对应关系！编号：" + row["Number"].ToString());
                            return;
                        }
                        E02A05P e02A05P = new E02A05P();
                        for (int i = 0; i < rs.Data.Tables[0].Columns.Count; i++) 
                        {
                            DataColumn dc = rs.Data.Tables[0].Columns[i];
                            ReflectUtil.setValue(e02A05P, dc.ColumnName, row[dc.ColumnName].ToString());
                        }

                        List<E02A05E> OrderList = new List<E02A05E>();

                        foreach (DataRow entry in rs.Data.Tables[1].Rows)
                        {
                            E02A05E e02A05E = new E02A05E();
                            for (int j = 0; j < rs.Data.Tables[1].Columns.Count; j++) 
                            {
                                DataColumn dc = rs.Data.Tables[1].Columns[j];
                                ReflectUtil.setValue(e02A05E, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            OrderList.Add(e02A05E);
                        }
                        e02A05P.ListIMEI = JsonConvert.SerializeObject(OrderList);
                        ht.Add(e02A05P);
                        result.Content = JsonConvert.SerializeObject(ht);
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("客户档案数据提取错误:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("客户档案数据提取错误:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 客户档案
    /// </summary>
    public class E02A05P
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 名称
        /// </summary>
        //public String Name;
        /// <summary>
        /// 建档日期
        /// </summary>
        public String BuildDate;
        /// <summary>
        /// 客户名称
        /// </summary>
        public String CusName;
        /// <summary>
        /// 密码
        /// </summary>
        public String PassWord;
        /// <summary>
        /// 联系人
        /// </summary>
        public String LinkMan;
        /// <summary>
        /// 备注
        /// </summary>
        public String Remark;
        /// <summary>
        /// 是否允许手机登录
        /// </summary>
        public Boolean IsApp;
        /// <summary>
        /// 是否只允许IMEI列表的设备访问
        /// </summary>
        public Boolean IsIMEI;
        /// <summary>
        /// 审批流程状态（）
        /// </summary>
        public int MultiCheckStatus;
        /// <summary>
        /// 
        /// </summary>
        public String ListIMEI;
        /// <summary>
        /// EAS财务组织
        /// </summary>
        public String FiUnitID;
     
    }
    /// <summary>
    /// 客户档案-IMEI列表
    /// </summary>
    public class E02A05E
    {
        public String BillId = String.Empty; //单据ID
        /// <summary>
        /// 设备IMEI
        /// </summary>
        public String IMEI;
        /// <summary>
        /// 设备IMEI顺序号
        /// </summary>
        public int Index;


    }
}
